from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import XMLOutputParser
# 必须导入xml的包，才能使用  pip install defusedxml

model = ChatOpenAI(model_name = "gpt-4o",temperature=0)

actory_query = "生成周星驰的简化电影作品列表，按照最新时间降序"

# 设置解析器 + 将指令注入模板
parser = XMLOutputParser(tags = ["movies","actor","film","name","genre"])
prompt = PromptTemplate(
    template="回答用户的查询\n{format_instructions}\n{query}\n",  # 将解析器的模块format_instructions，告诉提示词目标，这里的format_instructions 等于partial_variables中的format_instructions
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()},
)
print("format_instructions:",parser.get_format_instructions())


chain = prompt | model
response = chain.invoke({"query":actory_query})
print(response)
#print(parser.parse(response.content))